package com.shujia.scala;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Demo02WordCountJava {
    public static void main(String[] args) throws IOException {
        // 1、读取文件
        BufferedReader br = new BufferedReader(new FileReader("scala/data/words.txt"));
        String line;

        HashMap<String, Integer> wordCntMap = new HashMap<>();

        // 2、依次处理每一行数据
        while ((line = br.readLine()) != null) {
            // 3、切分每一条数据
            for (String word : line.split(",")) {
                // 4、判断单词是否存在于map中
                if (wordCntMap.containsKey(word)) {
                    // 存在就将value取出来并加一再写回去
                    wordCntMap.put(word, wordCntMap.get(word) + 1);
                } else {
                    // 不存在就直接将value置为1再写进去
                    wordCntMap.put(word, 1);
                }
            }
        }

        // 5、打印结果
        for (Map.Entry<String, Integer> kv : wordCntMap.entrySet()) {
            System.out.println(kv.getKey() + "," + kv.getValue());
        }
    }
}
